import { ElMessage } from "element-plus";

/**
 * 表格隔行变色
 * @param rowIndex
 * @return {string}
 */
export const tableRowClassName = ({ rowIndex }) => {
  return rowIndex % 2 === 0 ? "even-row" : "odd-row";
};

/**
 * 随机生成ID
 * @return {string}
 */
export const generateId = () => {
  const str = [];
  const hexDigits = "0123456789abcdef";
  for (let i = 0; i < 36; i++) {
    str[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  str[14] = "4";
  str[19] = hexDigits.substr((str[19] & 0x3) | 0x8, 1);
  return str.join("");
};

/**
 * 滚动到指定元素底部
 */
export const scrollToBottom = id => {
  const el = document.getElementById(id);
  const scrollHeight = el?.scrollHeight ?? 0;
  el.scrollTo(0, scrollHeight);
};

/**
 * 复制
 * @param value
 */
export const handelCopy = value => {
  const textarea = document.createElement("textarea");
  textarea.setAttribute("id", "copyTextarea");
  textarea.readOnly = "readonly";
  textarea.style.position = "absolute";
  textarea.style.left = "-9999px";
  textarea.value = value;
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand("Copy") && ElMessage.success("复制成功");
  document.body.removeChild(textarea);
};

/**
 * 文件链接下载
 * @param {string} url  -文件链接
 * @param {string} [name]  -文件名
 */
// export const downloadPDF = (url, name) => {
//   console.log(name,99999);
//   if (!url) {
//     return ElMessage.error("无文件链接");
//   }
//   try {
//     const fileName = name
//     console.log(name,99999999);
//     let link = document.createElement("a");
//     link.style.display = "none";
//     link.href = url;
//     link.setAttribute("download", fileName);
//     document.body.appendChild(link);
//     link.click();
//     setTimeout(() => {
//       URL.revokeObjectURL(link.href);
//       document.body.removeChild(link);
//     }, 1000);
//   } catch (err) {
//     alert(JSON.stringify(err));
//   }
// };

export const downloadPDF = (url, name) => {
  const xhr = new XMLHttpRequest();
  xhr.open("GET", url, true);
  xhr.responseType = "blob"; // 设置响应类型为blob，以便处理二进制数据

  xhr.onload = function () {
    if (xhr.status === 200) {
      const blob = xhr.response;
      const link = document.createElement("a");
      link.style.display = "none";
      link.href = window.URL.createObjectURL(blob);
      link.setAttribute("download", name); //文件名自更改
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  };
  xhr.send();
};

/**
 * 获取文件名
 * @param {String} fileUrl -地址
 */
export const getFileNameByUrl = fileUrl => {
  return fileUrl.slice(fileUrl.lastIndexOf("/") + 1);
};

/**
 * 拿到文件流后的下载的方法
 * @param {*} data
 * @param {*} filename
 * @param {*} mime
 * @param {*} bom
 */
export function downloadByData(data, filename = "签约合同压缩") {
  console.log(data);
  const blobData = [data];
  const blob = new Blob(blobData, { type: "application/octet-stream" });
  if (typeof window.navigator.msSaveBlob !== "undefined") {
    window.navigator.msSaveBlob(blob, filename + ".zip");
  } else {
    const blobURL = window.URL.createObjectURL(blob);
    console.log(blobURL);
    const tempLink = document.createElement("a");
    tempLink.style.display = "none";
    tempLink.href = blobURL;
    tempLink.setAttribute("download", filename + ".zip");
    if (typeof tempLink.download === "undefined") {
      tempLink.setAttribute("target", "_blank");
    }
    document.body.appendChild(tempLink);
    tempLink.click();
    document.body.removeChild(tempLink);
    window.URL.revokeObjectURL(blobURL);
  }
}
/**
 * 防抖
 */
export const _debounce = (fn, time) => {
  let timer = null;
  return (...args) => {
    const context = this;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.call(context, ...args);
    }, time);
  };
};